//
//  LSZCalculatorViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/9/1.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZCalculatorViewController.h"

@interface LSZCalculatorViewController ()
@property(nonatomic,strong) UIView * viewTop;
@property(nonatomic,strong) UIImageView * imageViewOne;
@property(nonatomic,strong) UIImageView * imageViewTwo;
@property(nonatomic,strong) UIImageView * imageViewThree;
@property(nonatomic,strong) UIImageView * imageViewFour;
@property(nonatomic,assign) NSInteger chooseType;//记录选择的是那个类型

@property(nonatomic,strong) UIImageView * imageViewMonthlySalary;//月薪
@property(nonatomic,strong) UIImageView * imageViewAnnualSalary;//年薪
@property(nonatomic,assign) NSInteger chooseSalaryType;//记录选择薪资是那个类型

@property(nonatomic,strong) UILabel * labelOptionalType;//税后工资label
@property(nonatomic,strong) UIView * viewMiddle;//税后工资view
@property(nonatomic,strong) UILabel * labelWage;//税后工资label
@property(nonatomic,strong) UIView * viewFollowing;//税后工资view
@property(nonatomic,strong) UIButton * buttonCalculate;//计算按钮
@property(nonatomic,strong) UITextField * textFieldCalculator;

@property(nonatomic,strong) UIView * viewPopup;
@property(nonatomic,strong) UIView * viewResults;
@end

@implementation LSZCalculatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.chooseType = -1;
    
    [self addOtherButton];
    
    self.navigationItem.title = @"党费计算器";
}
#pragma mark- 添加选择按钮
-(void)addOtherButton
{
    UILabel * labelType = [[UILabel alloc]init];
    labelType.frame = CGRectMake(10, NAVIGATION_BAR_HEIGHT+10, ScreenWidth, 20);
    labelType.text = @"类型";
    labelType.font = FONT_(14);
    labelType.textColor = titleColorLSZ;
    [self.view addSubview:labelType];
    
    self.viewTop = [[UIView alloc]init];
    self.viewTop.backgroundColor = [UIColor whiteColor];
    self.viewTop.frame = CGRectMake(0, CGRectGetMaxY(labelType.frame)+10, ScreenWidth, 100);
    [self.view addSubview:self.viewTop];
    
    UIButton * butOne = [[UIButton alloc]init];
    butOne.frame = CGRectMake(0, 0, self.viewTop.frame.size.width/2, self.viewTop.frame.size.height/2);
    butOne.tag = 1;
    [butOne addTarget:self action:@selector(clickChoose:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butOne];
    self.imageViewOne = [[UIImageView alloc]init];
    self.imageViewOne.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewOne.frame = CGRectMake(20, (butOne.frame.size.height-21)/2, 21, 21);
    [butOne addSubview:self.imageViewOne];
    UILabel * labelOne = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewOne.frame)+10, (butOne.frame.size.height-30)/2, self.viewTop.frame.size.width-CGRectGetMaxX(self.imageViewOne.frame)-10, 30)];
    labelOne.textColor = titleColorLSZ;
    labelOne.font = FONT_(15);
    labelOne.text = @"在职党员";
    [butOne addSubview:labelOne];
    
    UIButton * butTwo = [[UIButton alloc]init];
    butTwo.frame = CGRectMake(self.viewTop.frame.size.width/2, 0, self.viewTop.frame.size.width/2, self.viewTop.frame.size.height/2);
    butTwo.tag = 2;
    [butTwo addTarget:self action:@selector(clickChoose:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butTwo];
    self.imageViewTwo = [[UIImageView alloc]init];
    self.imageViewTwo.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewTwo.frame = CGRectMake(20, (butTwo.frame.size.height-21)/2, 21, 21);
    [butTwo addSubview:self.imageViewTwo];
    UILabel * labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewTwo.frame)+10, (butTwo.frame.size.height-30)/2, self.viewTop.frame.size.width-CGRectGetMaxX(self.imageViewTwo.frame)-10, 30)];
    labelTwo.textColor = titleColorLSZ;
    labelTwo.font = FONT_(15);
    labelTwo.text = @"离退休党员";
    [butTwo addSubview:labelTwo];
    
    UIButton * butThree = [[UIButton alloc]init];
    butThree.frame = CGRectMake(0, CGRectGetMaxY(butOne.frame), self.viewTop.frame.size.width/2, self.viewTop.frame.size.height/2);
    butThree.tag = 3;
    [butThree addTarget:self action:@selector(clickChoose:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butThree];
    self.imageViewThree = [[UIImageView alloc]init];
    self.imageViewThree.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewThree.frame = CGRectMake(20, (butThree.frame.size.height-21)/2, 21, 21);
    [butThree addSubview:self.imageViewThree];
    UILabel * labelThree = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewThree.frame)+10, (butThree.frame.size.height-30)/2, self.viewTop.frame.size.width-CGRectGetMaxX(self.imageViewThree.frame)-10, 30)];
    labelThree.textColor = titleColorLSZ;
    labelThree.font = FONT_(15);
    labelThree.text = @"农民党员";
    [butThree addSubview:labelThree];
    
    UIButton * butFour = [[UIButton alloc]init];
    butFour.frame = CGRectMake(self.viewTop.frame.size.width/2, CGRectGetMaxY(butTwo.frame), self.viewTop.frame.size.width/2, self.viewTop.frame.size.height/2);
    butFour.tag = 4;
    [butFour addTarget:self action:@selector(clickChoose:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butFour];
    self.imageViewFour = [[UIImageView alloc]init];
    self.imageViewFour.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewFour.frame = CGRectMake(20, (butFour.frame.size.height-21)/2, 21, 21);
    [butFour addSubview:self.imageViewFour];
    UILabel * labelFour = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewFour.frame)+10, (butFour.frame.size.height-30)/2, self.viewTop.frame.size.width-CGRectGetMaxX(self.imageViewFour.frame)-10, 30)];
    labelFour.textColor = titleColorLSZ;
    labelFour.font = FONT_(15);
    labelFour.text = @"学生党员";
    [butFour addSubview:labelFour];
    [self addOptionalOptions];
}
-(void)addOptionalOptions
{
    self.labelOptionalType = [[UILabel alloc]init];
    self.labelOptionalType.frame = CGRectMake(10, CGRectGetMaxY(self.viewTop.frame)+10, ScreenWidth, 20);
    self.labelOptionalType.font = FONT_(14);
    self.labelOptionalType.text = @"年薪/月薪";
    self.labelOptionalType.textColor = titleColorLSZ;
    [self.view addSubview:self.labelOptionalType];
    
    self.viewMiddle = [[UIView alloc]init];
    self.viewMiddle.frame = CGRectMake(0, CGRectGetMaxY(self.labelOptionalType.frame)+10, ScreenWidth, 50);
    self.viewMiddle.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewMiddle];
    
    UIButton * butMonthlySalary = [[UIButton alloc]init];
    butMonthlySalary.frame = CGRectMake(0, 0, self.viewMiddle.frame.size.width/2, self.viewMiddle.frame.size.height);
    butMonthlySalary.tag = 11;
    [butMonthlySalary addTarget:self action:@selector(clickChooseMonthly:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewMiddle addSubview:butMonthlySalary];
    self.imageViewMonthlySalary = [[UIImageView alloc]init];
    self.imageViewMonthlySalary.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewMonthlySalary.frame = CGRectMake(20, (butMonthlySalary.frame.size.height-21)/2, 21, 21);
    [butMonthlySalary addSubview:self.imageViewMonthlySalary];
    UILabel * labelOne = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewMonthlySalary.frame)+10, (butMonthlySalary.frame.size.height-30)/2, butMonthlySalary.frame.size.width-CGRectGetMaxX(self.imageViewMonthlySalary.frame)-10, 30)];
    labelOne.textColor = titleColorLSZ;
    labelOne.font = FONT_(15);
    labelOne.text = @"月薪";
    [butMonthlySalary addSubview:labelOne];
    
    UIButton * butTwo = [[UIButton alloc]init];
    butTwo.frame = CGRectMake(self.viewMiddle.frame.size.width/2, 0, self.viewMiddle.frame.size.width/2, self.viewMiddle.frame.size.height);
    butTwo.tag = 12;
    [butTwo addTarget:self action:@selector(clickChooseMonthly:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewMiddle addSubview:butTwo];
    self.imageViewAnnualSalary = [[UIImageView alloc]init];
    self.imageViewAnnualSalary.image = [UIImage imageNamed:@"党建_未选中"];
    self.imageViewAnnualSalary.frame = CGRectMake(20, (butTwo.frame.size.height-21)/2, 21, 21);
    [butTwo addSubview:self.imageViewAnnualSalary];
    UILabel * labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageViewAnnualSalary.frame)+10, (butTwo.frame.size.height-30)/2, butTwo.frame.size.width-CGRectGetMaxX(self.imageViewAnnualSalary.frame)-10, 30)];
    labelTwo.textColor = titleColorLSZ;
    labelTwo.font = FONT_(15);
    labelTwo.text = @"年薪";
    [butTwo addSubview:labelTwo];
    
    
    
    self.labelWage = [[UILabel alloc]init];
    self.labelWage.frame = CGRectMake(10, CGRectGetMaxY(self.viewMiddle.frame)+10, ScreenWidth, 20);
    self.labelWage.font = FONT_(14);
    self.labelWage.text = @"税后工资";
    self.labelWage.textColor = titleColorLSZ;
    [self.view addSubview:self.labelWage];
    
    self.viewFollowing = [[UIView alloc]init];
    self.viewFollowing.frame = CGRectMake(0, CGRectGetMaxY(self.labelWage.frame)+10, ScreenWidth, 50);
    self.viewFollowing.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewFollowing];
    
    UIImageView * imageViewCalculator = [[UIImageView alloc]init];
    imageViewCalculator.frame = CGRectMake(10, (self.viewFollowing.frame.size.height-21)/2, 21, 21);
    imageViewCalculator.image = [UIImage imageNamed:@"计算器"];
    [self.viewFollowing addSubview:imageViewCalculator];
    
    self.textFieldCalculator = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageViewCalculator.frame)+10, 5, ScreenWidth-CGRectGetMaxX(imageViewCalculator.frame)-20, self.viewFollowing.frame.size.height-10)];
    self.textFieldCalculator.placeholder = @"输入工资数";
    [self.viewFollowing addSubview:self.textFieldCalculator];
    
    
    self.buttonCalculate = [[UIButton alloc]init];
    self.buttonCalculate.frame = CGRectMake(20, CGRectGetMaxY(self.viewFollowing.frame)+40, ScreenWidth-40, 50);
    self.buttonCalculate.backgroundColor = themeColor;
    [self.buttonCalculate.layer setMasksToBounds:YES];
    [self.buttonCalculate.layer setCornerRadius:4];
    [self.buttonCalculate setTitle:@"开始计算" forState:UIControlStateNormal];
    [self.buttonCalculate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.buttonCalculate addTarget:self action:@selector(clickButCalculate) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonCalculate];
}
#pragma mark - 开始计算
-(void)clickButCalculate
{
    if (self.chooseType == -1) {
        [self initWithtring:@"请选择类型!"];
        return;
    }
    else if (self.chooseType == 3 ) {
        [self popupWindow:@"0.2 - 1"];//直接计算

    }else if ( self.chooseType == 4){
        [self popupWindow:@"0.2"];//直接计算
    }
    else{
        if (self.chooseSalaryType == 0) {
            [self initWithtring:@"请选择薪资类型!"];
            return;
        }
        if ([(NullStringjudgment(self.textFieldCalculator.text)) isEqualToString:@""]) {
            [self initWithtring:@"请输入税后工资!"];
            return;
        }
        if(self.chooseSalaryType == 12){
           CGFloat size = [self.textFieldCalculator.text floatValue]/12;
            CGFloat results  = [self CalculateTheAmountOfMoneyToBePaid:size];
            NSString * str = [NSString stringWithFormat:@"%.4f",results];
            [self popupWindow:str];//直接计算
        }else{
            CGFloat results  = [self CalculateTheAmountOfMoneyToBePaid:[self.textFieldCalculator.text floatValue] ];
            NSString * str = [NSString stringWithFormat:@"%.4f",results];
            [self popupWindow:str];//直接计算
        }
    }
}
-(CGFloat)CalculateTheAmountOfMoneyToBePaid :(CGFloat)size
{
    CGFloat value;
    if (size <= 3000) {
        value = size * 0.005;
    }else if (size <= 5000){
        value = size * 0.01;
    }else if (size <= 10000){
        value = size * 0.015;
    }else{
        value = size * 0.02;
    }
    return value;
}
#pragma mark - 选择薪资
-(void)clickChooseMonthly:(UIButton *)button
{
    self.chooseSalaryType = button.tag;
    if (button.tag == 11) {
        self.imageViewMonthlySalary.image = [UIImage imageNamed:@"党建_选中"];
        self.imageViewAnnualSalary.image = [UIImage imageNamed:@"党建_未选中"];

    }else{
        self.imageViewMonthlySalary.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewAnnualSalary.image = [UIImage imageNamed:@"党建_选中"];
    }
    
}
#pragma mark - 选择类型
-(void)clickChoose:(UIButton *)button
{
    self.chooseType = button.tag;
    
    if (button.tag == 1) {
        self.imageViewOne.image = [UIImage imageNamed:@"党建_选中"];
        self.imageViewTwo.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewThree.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewFour.image = [UIImage imageNamed:@"党建_未选中"];
        [self isOrHidden:1];
    }else if (button.tag == 2){
        self.imageViewOne.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewTwo.image = [UIImage imageNamed:@"党建_选中"];
        self.imageViewThree.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewFour.image = [UIImage imageNamed:@"党建_未选中"];
        [self isOrHidden:1];
    }else if (button.tag == 3){
        self.imageViewOne.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewTwo.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewThree.image = [UIImage imageNamed:@"党建_选中"];
        self.imageViewFour.image = [UIImage imageNamed:@"党建_未选中"];
        [self isOrHidden:2];
    }else  {
        self.imageViewOne.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewTwo.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewThree.image = [UIImage imageNamed:@"党建_未选中"];
        self.imageViewFour.image = [UIImage imageNamed:@"党建_选中"];
        [self isOrHidden:2];
    }
}
-(void)isOrHidden:(NSInteger)number
{
    if (number == 1) {
        self.labelOptionalType.hidden = NO;
        self.viewMiddle.hidden = NO;
        self.labelWage.hidden = NO;
        self.viewFollowing.hidden = NO;
        CGRect rect = self.buttonCalculate.frame;
        rect.origin.y = CGRectGetMaxY(self.viewFollowing.frame)+40;
        self.buttonCalculate.frame = rect;
    }else{
        self.labelOptionalType.hidden = YES;
        self.viewMiddle.hidden = YES;
        self.labelWage.hidden = YES;
        self.viewFollowing.hidden = YES;
        CGRect rect = self.buttonCalculate.frame;
        rect.origin.y = CGRectGetMaxY(self.viewTop.frame)+40;
        self.buttonCalculate.frame = rect;

    }
}
-(void)popupWindow:(NSString *)moneyStr
{
    self.viewPopup = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.viewPopup.backgroundColor = [UIColor blackColor];
    self.viewPopup.alpha = 0.7;
    UITapGestureRecognizer *click = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(butCancel)];
    [self.viewPopup addGestureRecognizer:click];
    [self.view addSubview:self.viewPopup];
    
    self.viewResults = [[UIView alloc]init];
    self.viewResults.frame = CGRectMake(20,(ScreenHeight-250)/2, ScreenWidth-40, 250);
    self.viewResults.backgroundColor = [UIColor whiteColor];
    [self.viewResults.layer setMasksToBounds:YES];
    [self.viewResults.layer setCornerRadius:4];
    [self.view addSubview:self.viewResults];
    
    UIButton * buttonCancel = [[UIButton alloc]init];
    buttonCancel.frame = CGRectMake(self.viewResults.frame.size.width-21-10, 10, 21, 21);
    [buttonCancel setImage:[UIImage imageNamed:@"取消"] forState:UIControlStateNormal];
    [buttonCancel addTarget:self action:@selector(butCancel) forControlEvents:UIControlEventTouchUpInside];
    [self.viewResults addSubview:buttonCancel];
    
    UILabel * labelTitle = [[UILabel alloc]init];
    labelTitle.frame = CGRectMake(0, 25, self.viewResults.frame.size.width, 35);
    labelTitle.text = @"缴费结果";
    labelTitle.font = FONT_(22);
    labelTitle.textAlignment = NSTextAlignmentCenter;
    [self.viewResults addSubview:labelTitle];
    
    UIImageView * imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"薪资标志钱"];
    imageView.frame = CGRectMake((self.viewResults.frame.size.width-66)/2, CGRectGetMaxY(labelTitle.frame)+15, 66, 66);
    [self.viewResults addSubview:imageView];
    
    UILabel * labelPrompt = [[UILabel alloc]init];
    labelPrompt.frame = CGRectMake(0, CGRectGetMaxY(imageView.frame)+15, self.viewResults.frame.size.width, 20);
    labelPrompt.textColor = titleColorLSZ;
    labelPrompt.font = FONT_(15);
    labelPrompt.text = @"您要缴纳的党费每月是";
    labelPrompt.textAlignment = NSTextAlignmentCenter;
    [self.viewResults addSubview:labelPrompt];
    
    UILabel * labelMoney = [[UILabel alloc]init];
    labelMoney.frame = CGRectMake(0, CGRectGetMaxY(labelPrompt.frame)+15, self.viewResults.frame.size.width, 40);
    labelMoney.font = FONT_(18);
    labelMoney.textColor = themeColor;
    labelMoney.text = [NSString stringWithFormat:@"%@元",moneyStr];
    labelMoney.textAlignment = NSTextAlignmentCenter;
    [self.viewResults addSubview:labelMoney];
    
}
-(void)butCancel
{
    [self.viewPopup removeFromSuperview];
    [self.viewResults removeFromSuperview];
}
@end
